#include "stdafx.h"

#include "SharpSDL.h"
#include "CDROM.h"

using namespace SharpSDL::ManagedSDL;

int SharpSDL::ManagedSDL::CDROMDrive::CDROMDriveNum(void)
{
	int ret = SDL_CDNumDrives();
	if (ret <= -1)
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	return ret;
}

System::String ^SharpSDL::ManagedSDL::CDROMDrive::CDROMDriveName(int DriveNumber)
{
	System::String ^ret;
	if ((DriveNumber < 0) || (DriveNumber >= SharpSDL::ManagedSDL::CDROMDrive::CDROMDriveNum()))
	{
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
		ret = gcnew System::String("");
	} else {
		ret = gcnew System::String((char*)SDL_CDName(DriveNumber));
	}
	return ret;
}

SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus SharpSDL::ManagedSDL::CDROMDrive::GetCDROMStatus(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		switch (SDL_CDStatus(CurrentCDROM))
		{
		case CDstatus::CD_PAUSED:
			return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Paused;
			break;
		case CDstatus::CD_PLAYING:
			return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Playing;
			break;
		case CDstatus::CD_STOPPED:
			return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Stopped;
			break;
		case CDstatus::CD_TRAYEMPTY:
			return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Trayempty;
			break;
		default:
			return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Error;
			break;
		}
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
		return SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Error;
	}
}

bool SharpSDL::ManagedSDL::CDROMDrive::CDInDrive(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		return ((GetCDROMStatus() != SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Error) && (GetCDROMStatus() != SharpSDL::ManagedSDL::CDROMDrive::CDROMStatus::Trayempty));
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
		return false;
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::Eject(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		SDL_CDEject(CurrentCDROM);
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::Open(int DriveNumber)
{
	if ((DriveNumber < 0) || (DriveNumber >= SharpSDL::ManagedSDL::CDROMDrive::CDROMDriveNum()))
	{
		Status = SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Closed;
	} else {
		CurrentCDROM = SDL_CDOpen(DriveNumber);
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
		Status = SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened;
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::PlayTracks(int StartTrack, int StartFrame, int nTracks, int nFrames)
{
	if (SDL_CDPlayTracks(CurrentCDROM, StartTrack, StartFrame, nTracks, nFrames) == -1)
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
}

void SharpSDL::ManagedSDL::CDROMDrive::Play(int Start, int Length)
{
	if (SDL_CDPlay(CurrentCDROM, Start, Length) == -1)
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
}

void SharpSDL::ManagedSDL::CDROMDrive::Pause(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		SDL_CDPause(CurrentCDROM);
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::Resume(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		SDL_CDResume(CurrentCDROM);
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::Stop(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		SDL_CDStop(CurrentCDROM);
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	}
}

void SharpSDL::ManagedSDL::CDROMDrive::Close(void)
{
	if (Status == SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Opened)
	{
		SDL_CDClose(CurrentCDROM);
		Status = SharpSDL::ManagedSDL::CDROMDrive::CDROMInitStatus::Closed;
	} else {
		SharpSDL::SDL::Error::SetError(SharpSDL::SDL::Error::ErrorCode::CDROMERROR);
	}
}

SharpSDL::ManagedSDL::CDROMDrive::~CDROMDrive(void)
{
	Close();
}

SharpSDL::ManagedSDL::CDROMDrive::CDROMDrive(int DriveNumber)
{
	Open(DriveNumber);
}